<?php
namespace Cmf\Controllers;

use Cmf\Models\Menu;
use Cmf\Forms\MenuForm;
use Phalcon\Http\Response;
use Phalcon\Mvc\Model\Criteria;
use Phalcon\Paginator\Adapter\Model as Paginator;

/**
 * 菜单管理控制器
 */
class MenuController extends BaseController
{
    public function indexAction()
    {
        $menu = Menu::find([
            'conditions' => 'pid = :pid:', 
            'bind' => ['pid' => 0]
        ]);

        $data = Menu::find([
            'order' => 'id DESC', 
            'columns' => 'id,title,url,is_group,is_hide' 
        ]);

        $paginator = new Paginator([
            'data' => $data, 
            'page' => $this->request->getQuery('page', 'int'), 
            'limit' => 10
        ]);

        $this->view->page = $paginator->getPaginate();
        $this->view->item = $menu;
    }
    
    public function searchAction()
    {
        if ($this->request->isGet()) {
            $query = Criteria::fromInput($this->di, "Cmf\Models\Menu", $this->request->getQuery());
            $this->persistent->searchParams = $query->getParams();
        }
        
        $parameters = array();
        if ($this->persistent->searchParams) {
            $parameters = $this->persistent->searchParams;
            
            if (!empty($this->request->getPost('startDate')) && !empty($this->request->getQuery('endDate'))) {
                $parameters["conditions"].= " AND [created_at] between :startDate: AND :endDate:";
                $parameters["bind"]['endDate'] = strtotime($this->request->getQuery('endDate'));
                $parameters["bind"]['startDate'] = strtotime($this->request->getQuery('startDate'));
            }
        }
        
        $menu = Menu::find([
            'conditions' => 'pid = :pid:', 
            'bind' => ['pid' => 0]
        ]);

        $data = Menu::find($parameters);

        $paginator = new Paginator([
            'data' => $data, 
            'limit' => 10, 
            'page' => $this->request->getQuery('page', 'int') ? : 1
        ]);
        
        $this->view->pid = $this->request->getQuery('pid');
        $this->view->page = $paginator->getPaginate();
        $this->view->item = $menu;
    }
    
    public function newAction()
    {
        $form = new MenuForm();
        $this->view->form = $form;
    }
    
    public function createAction()
    {
        if ($this->request->isPost()) {
            $menu = new Menu();
            $form = new MenuForm;
            
            if ($form->isValid($this->request->getPost()) === false) {
                foreach ($form->getMessages() as $key => $message) {
                    $this->flash->error($message);
                }
                
                return $this->dispatcher->forward('menu/new');
            }
            
            if (!$menu->create($this->request->getPost())) {
                foreach ($menu->getMessages() as $message) {
                    $this->flash->error($message);
                }
                
                return $this->dispatcher->forward('menu/new');
            }
            
            $this->flash->success('新增菜单成功');
            
            return $this->response->redirect('menu/index');
        }
    }
    
    public function editAction()
    {
        $menu = Menu::findFirstById($this->request->getQuery('id', 'int'));
        $form = new MenuForm($menu, 'edit');
        $this->view->form = $form;
    }
    
    public function updateAction()
    {
        if ($this->request->isPost()) {
            $menu = new Menu();
            $form = new MenuForm;
            
            if ($form->isValid($this->request->getPost()) === false) {
                foreach ($form->getMessages() as $key => $message) {
                    $this->flash->error($message);
                }
                
                return $this->dispatcher->forward('menu/edit');
            }
            
            if (!$menu->save($this->request->getPost())) {
                foreach ($menu->getMessages() as $message) {
                    $this->flash->error($message);
                }
                
                return $this->dispatcher->forward('menu/edit');
            }
            
            $this->flash->success('编辑菜单成功');
            
            return $this->response->redirect('menu/index');
        }
    }
    
    public function deleteAction()
    {
        $this->view->disable();

        $menu = Menu::findFirstById($this->request->getPost('id'));
        $response = new Response();
        
        if (!$menu) {
            $response->setContent(json_encode(['message' => '菜单不存在']));
            
            return $response;
        }
        
        if (!$menu->delete()) {
            foreach ($menu->getMessage() as $message) {
                $this->flash->error($message);
            }
            $response->setContent(json_encode(['message' => '菜单删除失败']));
            
            return $response;
        }
        
        $response->setContent(json_encode(['message' => '删除菜单成功', 'id' => $this->request->getPost('id') ]));
        
        return $response;
    }
}
